﻿using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

/// <summary>
/// 签到系统
/// </summary>
public class SignPanel : MonoBehaviour {

    public const string CountPrefs = "SignCount";
    public const string SignDate = "SignDate";

    public GameObject[] signImages;
    public GameObject signButtion;

    string url = "http://cgi.im.qq.com/cgi-bin/cgi_svrtime";
    DateTime today;

    IEnumerator Start () {
        //提前隐藏按钮，防止作弊
        signButtion.SetActive(false);

        WWW www = new WWW(url);
        yield return www;

        if(www.error==null)
        {
            today = Convert.ToDateTime(www.text);
            RefreshUI();
            string dateStr = PlayerPrefs.GetString(SignDate);
            if (dateStr != "")
            {
                DateTime signDate = Convert.ToDateTime(dateStr);
                if (DateEqual(signDate, today)==false)
                {
                    signButtion.SetActive(true);
                }
            }
            else//一次都没有签过
            {
                signButtion.SetActive(true);
            }
        }
	}
	
    //签到按钮点击
	public void OnSignBtnClick () {
        signButtion.SetActive(false);

        int count = PlayerPrefs.GetInt(CountPrefs, 0);
        count++;
        PlayerPrefs.SetInt(CountPrefs,count);
        PlayerPrefs.SetString(SignDate,today.ToShortDateString());
        //签到送金币，
        RefreshUI();
    }

    void RefreshUI()
    {
        //获取已签到第几天
        int count = PlayerPrefs.GetInt(CountPrefs, 0);
        for (int i = 0; i < count; i++)
        {
            signImages[i].GetComponent<Image>().color = Color.red;
        }
    }

    //判断日期是否相等，
    bool DateEqual(DateTime t1, DateTime t2)
    {
        bool equal = t1.Year == t2.Year && t1.Month == t2.Month && t1.Day == t2.Day;

        return equal;
    }
}
